<?php
namespace Admin\Model;
use Think\Model;
class MemberModel extends Model 
{
	protected $insertFields = array('code','username','sex','telphone','jifen','level','status','kahao','kaxiang','class_count','member_cat','member_hobby','work_man','intro_man','member_account','member_zk','member_count','class_in_count','class_account','start_time','end_time','pay_account','payed','pay_last','from','shengfenzheng','addr','requirement','email','weixinorqq','birthday','comment','card_id','company_id','pay_way');
	protected $updateFields = array('id','code','username','sex','telphone','jifen','level','status','kahao','kaxiang','class_count','member_cat','member_hobby','work_man','intro_man','member_account','member_zk','member_count','class_in_count','class_account','start_time','end_time','pay_account','payed','pay_last','from','shengfenzheng','addr','requirement','email','weixinorqq','birthday','comment','card_id','pay_way');
	protected $_validate = array(
		array('code', 'number', '会员编号必须是一个整数！', 2, 'regex', 3),
		array('username', 'require', '姓名不能为空！', 1, 'regex', 3),
		array('username', '1,30', '姓名的值最长不能超过 30 个字符！', 1, 'length', 3),
		array('telphone', '1,50', '手机的值最长不能超过 50 个字符！', 2, 'length', 3),
		array('jifen', 'number', '积分必须是一个整数！', 2, 'regex', 3),
		array('level', 'number', '级别必须是一个整数！', 2, 'regex', 3),
		array('status', 'number', '状态必须是一个整数！', 2, 'regex', 3),
		array('kahao', 'number', '卡号必须是一个整数！', 2, 'regex', 3),
		array('kaxiang', '1,150', '卡项的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('class_count', '1,150', '选课数的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('member_cat', '1,150', '分类的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('member_hobby', '1,150', '专修的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('work_man', '1,150', '员工的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('intro_man', '1,150', '介绍人的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('member_account', '1,150', '余额的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('member_zk', '1,150', '折扣的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('member_count', '1,150', '总次数的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('class_in_count', '1,150', '上课次数的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('class_account', '1,150', '余次的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('start_time', '1,150', '开始时间的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('end_time', '1,150', '结束时间的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('pay_account', '1,150', '应付款的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('payed', '1,150', '已付的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('pay_last', '1,150', '欠款的值最长不能超过 150 个字符！', 2, 'length', 3),
		array('from', '1,150', '来源的值最长不能超过 150 个字符！', 2, 'length', 3),
	);
	public function search($pageSize = 5)
	{
		/**************************************** 搜索 ****************************************/
		$where = array();
		if($code = I('get.code'))
			$where['code'] = array('eq', $code);
		if($username = I('get.username'))
			$where['username'] = array('like', "%$username%");
		if($telphone = I('get.telphone'))
			$where['telphone'] = array('like', "%$telphone%");
		if($intro_man = I('get.intro_man'))
			$where['intro_man'] = array('like', "%$intro_man%");
		$start_timefrom = I('get.start_timefrom');
		$start_timeto = I('get.start_timeto');
		if($start_timefrom && $start_timeto)
			$where['start_time'] = array('between', array(strtotime("$start_timefrom 00:00:00"), strtotime("$start_timeto 23:59:59")));
		elseif($start_timefrom)
			$where['start_time'] = array('egt', strtotime("$start_timefrom 00:00:00"));
		elseif($start_timeto)
			$where['start_time'] = array('elt', strtotime("$start_timeto 23:59:59"));
		$start_timefrom = I('get.start_timefrom');
		$start_timeto = I('get.start_timeto');
		if($start_timefrom && $start_timeto)
			$where['end_time'] = array('between', array(strtotime("$start_timefrom 00:00:00"), strtotime("$start_timeto 23:59:59")));
		elseif($start_timefrom)
			$where['end_time'] = array('egt', strtotime("$start_timefrom 00:00:00"));
		elseif($start_timeto)
			$where['end_time'] = array('elt', strtotime("$start_timeto 23:59:59"));
		if($from = I('get.from'))
			$where['from'] = array('like', "%$from%");
        //对应瑜伽馆
        if(intval(session('admin.company_id')) !==1){
            $where['company_id'] = ['eq',session('admin.company_id')];
        }
		/************************************* 翻页 ****************************************/
		$count = $this->alias('a')->where($where)->count();
		$page = new \Think\Page($count, $pageSize);
		// 配置翻页的样式
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$data['page'] = $page->show();
		/************************************** 取数据 ******************************************/
		$data['data'] = $this->alias('a')->where($where)->group('a.id')->limit($page->firstRow.','.$page->listRows)->select();
		return $data;
	}
	// 添加前
	protected function _before_insert(&$data, $option)
	{
		if(isset($_FILES['face']) && $_FILES['face']['error'] == 0)
		{
			$ret = uploadOne('face', 'Member', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['face'] = $ret['images'][0];
				$data['big_face'] = $ret['images'][1];
				$data['mid_face'] = $ret['images'][2];
				$data['sm_face'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
		}
        $data['company_id'] = session('admin.company_id');
	}
	// 修改前
	protected function _before_update(&$data, $option)
	{
		if(isset($_FILES['face']) && $_FILES['face']['error'] == 0)
		{
			$ret = uploadOne('face', 'Member', array(
				array(350, 350, 2),
				array(150, 150, 2),
				array(50, 50, 2),
			));
			if($ret['ok'] == 1)
			{
				$data['face'] = $ret['images'][0];
				$data['big_face'] = $ret['images'][1];
				$data['mid_face'] = $ret['images'][2];
				$data['sm_face'] = $ret['images'][3];
			}
			else 
			{
				$this->error = $ret['error'];
				return FALSE;
			}
			deleteImage(array(
				I('post.old_face'),
				I('post.old_big_face'),
				I('post.old_mid_face'),
				I('post.old_sm_face'),
	
			));
		}
        $data['company_id'] = session('admin.company_id');
    }
	// 删除前
	protected function _before_delete($option)
	{
		if(is_array($option['where']['id']))
		{
			$this->error = '不支持批量删除';
			return FALSE;
		}
		$images = $this->field('face,big_face,mid_face,sm_face')->find($option['where']['id']);
		deleteImage($images);
	}
	/************************************ 其他方法 ********************************************/
}